/*
 * Copyright (C) 2008-2011 University of Deusto
 * 
 * All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.
 * 
 * This software consists of contributions made by many individuals, 
 * listed below:
 *
 * Author: Aitor Gómez Goiri <aitor.gomez@deusto.es>
 */
package otso.se.network.sockets.communication.abstraction;

import java.io.Serializable;

import org.restlet.data.Status;

public class HttpResponse implements Serializable {
	// e.g. HTTP/1.1 200 OK
	private transient Status status;
	final private int statusCode; // serializable
	
	// e.g. long timestamp (in HTTP "Date: Tue, 05 Jul 2011 17:58:23 GMT")
	final private long date;
	//Accept-Ranges: bytes
	//Server: Restlet-Framework/2.0.5
	//Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
	
	// e.g. Content-Length: 135
	private int contentLength;
	// e.g. Content-Type: application/json; charset=UTF-8
	private String contentType;
	
	private byte[] data;
	
	public HttpResponse(Status status, long date) {
		this.status = status;
		this.date = date;
		this.statusCode = status.getCode();
	}

	public int getContentLength() {
		return contentLength;
	}

	private void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Status getStatus() {
		if(this.status==null) // for serialized HttpResponse objects...
			this.status = Status.valueOf(this.statusCode);
		return this.status;
	}

	public long getDate() {
		return date;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data.getBytes();
		setContentLength(this.data.length);
	}

	@Override
	public String toString() {
		return "HttpResponse [statusCode=" + statusCode + ", data="
				+ new String(data) + "]";
	}
}